/*!
 *
 * 
 */

(function(window, undefined) {
	var Desktop = window.Desktop = function(id, context){
		if ( !(this instanceof window.Desktop) ) {
			return new window.Desktop( id );
		}
		
		this.id = id;
		this.elemId = this.id;
		this.init();
		this.apps = {};
		this.states = {needflush:true};
		
	};
	
	Desktop.fn = Desktop.prototype = {
		constructor: Desktop,
		
		init: function(){
			var desktop = this;
			desktop.loaded=false;
			$desk = $(Desktop.template).clone();
			$desk.attr("id", desktop.id);
			desktop.elem = $desk[0];
			
			$(window).bind("resize", function(){
				desktop.resize();
			});
			
			desktop.appZIndexChain = Desktop.chainSupport.createChain();
		},
		onload: function(event, destContainer){
			var desktop = event.target;
			if(desktop.loaded){
				warn("[desktop][load]Desktop has loaded before."); return;
			}
			desktop.loaded = true;
			$(desktop.elem).appendTo(destContainer);
		},
		loadapp: function( app ) {
			var desktop = this;
			$(app).triggerHandler('load',[this, $(this.elem).find("ul.deskmain"),
			                       $(this.elem).find("div.appViewport"), $("#taskItemBox")]);
			this.apps[app.id] = app;
			
			$(app).bind("focus", function(event){
				info("event:", event.type);
				var _app = event.target; 
				Desktop.chainSupport.prepose(desktop.appZIndexChain, _app.id);
			});
			
			$(app).bind("minisize close", function(event){
				info("event:", event.type);
				var _app = event.target; 
				Desktop.chainSupport.remove(desktop.appZIndexChain, _app.id);
				var _curAppId = Desktop.chainSupport.first(desktop.appZIndexChain);
				if(_curAppId)desktop.apps[_curAppId].focus();
			});
			
			
			return this;
		},
		turninto:function(origEvent){
			var desktop = this;
			if(desktop.states.needflush)desktop.flushApp();
			$(desktop.elem).addClass("subdesk_current");
			$(desktop).triggerHandler('turninto');
			
			if(!(origEvent && origEvent.target instanceof window.App)){
				var _curAppId = Desktop.chainSupport.first(desktop.appZIndexChain);
				if(_curAppId)desktop.apps[_curAppId].focus();
			}
			
			$(desktop.elem).animate({left: '0px'},250,function(){
				$(desktop.elem).addClass("finishturninto");
				$(desktop).triggerHandler('finishturninto');
			});
		},
		turnleave: function(){
			var desktop = this;
			$(desktop.elem).css({left: '2000px'});
			$(desktop.elem).removeClass("subdesk_current");
			$(desktop.elem).removeClass("finishturninto");
			
			var _curAppId = Desktop.chainSupport.first(desktop.appZIndexChain);
			if(_curAppId)desktop.apps[_curAppId].unfocus();
			
			$(desktop).triggerHandler('turnleave');
		},
		addFinishturnintoCallback: function(key, fn){
			var desktop = this;
			if(!desktop._once_tasklist){
				desktop._once_tasklist = {};
				desktop._once_tasklist.key = [fn];
			} else{
				if(desktop._once_tasklist.key){
					desktop._once_tasklist.key.push(fn);
				} else{
					delete desktop._once_tasklist;
					desktop._once_tasklist = {};
					desktop._once_tasklist.key = [fn];
				}
			}
		},
		onfinishturninto: function(event){
			var desktop = event.target;
			if(desktop._once_tasklist){
					for(var i in desktop._once_tasklist.key){
						desktop._once_tasklist.key[i]();
					}
					delete desktop._once_tasklist;
				}
		},
		isCurrent:function(){
			return $("#"+this.elemId).hasClass("subdesk_current");
		},
		isFinishturninto:function(){
			return $("#"+this.elemId).hasClass("finishturninto");
		},
		flushApp:function(){
			var desktop = this;
			for(var id in desktop.apps){
				desktop.apps[id].autoPosition();
			}
			desktop.states.needflush = false;
		},
		resize:function(){
			var desktop = this;
			desktop.states.needflush = true;
			if(desktop.isCurrent()){
				desktop.flushApp();
			}
		}
	};
	
	Desktop.template =  '<div class="subdesk">'
			+ '<ul class="deskmain" style="left:73px; right:0;"></ul>'
			+ '<div class="appViewport"></div>'
			+ '</div>';
	
	Desktop.chainSupport = {
		createChain:function(){
			return new Array();
		},
		prepose: function(chain, v){
			debug("[Desktop.chainSupport][prepose] origin:", chain);
			this._remove(chain, v);
			chain.unshift(v);
			debug("[Desktop.chainSupport][prepose] result:", chain);
		},
		remove:function(chain, v){
			debug("[Desktop.chainSupport][remove] origin:", chain);
			this._remove(chain, v);
			debug("[Desktop.chainSupport][remove] result:", chain);
		},
		_remove:function(chain, v){
			for(var i in chain){
				if(v == chain[i]){
					chain.splice(i,1);
					return;
				}
			}
		},
		first:function(chain){
			return chain[0];
		}
		
	};
})(window);
	
